package com.yachang.qym.usbhost.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.TextView;

import com.bcgtgjyb.huanwen.customview.mylibrary.FiveLine;
import com.yachang.qym.usbhost.R;
import com.yachang.qym.usbhost.model.ContactModel;
import com.yachang.qym.usbhost.model.OutPoliceModel;
import com.yachang.qym.usbhost.utils.AllData;
import com.yachang.qym.usbhost.utils.DataTransfer;
import com.yachang.qym.usbhost.utils.LoadImage;
import com.yachang.qym.usbhost.utils.VoiceUtil;

import de.hdodenhof.circleimageview.CircleImageView;

public class CallingActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private TextView tvIsListen;
    private TextView tvIsStyle;
    private CircleImageView ivHead;

    private FiveLine animListen;
    private TextView tvName;

    public boolean threadCt = false;
    private Handler mHandler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what) {
                case 0: {
                    VoiceUtil.isNormal = true;
                    CallingActivity.this.finish();
                }break;
                default:break;
            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calling);
        initView();

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    private void initView() {

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("通话中");

        tvIsListen = (TextView) findViewById(R.id.tv_is_listen);
        tvIsStyle = (TextView) findViewById(R.id.tv_is_style);
        animListen = (FiveLine) findViewById(R.id.anim_listen);
        ivHead = (CircleImageView) findViewById(R.id.iv_head);
        tvName = (TextView) findViewById(R.id.tv_name);
        animListen.start();
        bingData();
    }

    private void bingData() {
        ContactModel contactModel = new ContactModel();
        for(int i = 0; i < AllData.contactModels.size(); i++){
            if(DataTransfer.byteEquals(VoiceUtil.getID(),AllData.contactModels.get(i).getCallID())){
                contactModel = AllData.contactModels.get(i);
                break;
            }
        }
        OutPoliceModel outPoliceModel = new OutPoliceModel();
        if(VoiceUtil.style == 1){
            for (int i = 0; i < AllData.outPoliceModels.size(); i++) {
                if (AllData.outPoliceModels.get(i).getMid().equals(DataTransfer.getTenNum(VoiceUtil.getID()) + "")) {
                    outPoliceModel = AllData.outPoliceModels.get(i);
                    LoadImage.showImage(outPoliceModel.getUserImage(), ivHead);
                    break;
                }
            }
        } else {
            for (int i = 0; i < AllData.outPoliceModels.size(); i++) {
                if (VoiceUtil.mid.equals(AllData.outPoliceModels.get(i).getMid())) {
                    outPoliceModel = AllData.outPoliceModels.get(i);
                    LoadImage.showImage(outPoliceModel.getUserImage(), ivHead);
                    break;
                }
            }
        }
        tvName.setText(outPoliceModel.getUserName());
        switch (VoiceUtil.style) {
            case 0:
                tvIsStyle.setText("组呼");
                if(VoiceUtil.whatIsDoing == VoiceUtil.IS_SPEAK){
                    tvIsListen.setText("呼叫：" + contactModel.getName());
                }else {
                    tvIsListen.setText("来自：" + contactModel.getName());
                }
                break;
            case 1:
                tvIsStyle.setText("私密呼叫");
                if(VoiceUtil.whatIsDoing == VoiceUtil.IS_SPEAK){
                    tvIsListen.setText("呼叫：" + outPoliceModel.getUserName());
                }else {
                    tvIsListen.setText("来自：" + outPoliceModel.getUserName());
                }
                break;
            case 2:
                tvIsStyle.setText("全体呼叫");
                if(VoiceUtil.whatIsDoing == VoiceUtil.IS_SPEAK){
                    tvIsListen.setText("呼叫：全体");
                }else {
                    tvIsListen.setText("来自：全体" );
                }
                break;
            default:break;
        }
        threadCt = true;
        GetVoiceChange thread = new GetVoiceChange();
        thread.start();
    }

    private class GetVoiceChange extends Thread{
        public void run(){
            while (threadCt) {
                if(!VoiceUtil.isNormal) {
                    if (VoiceUtil.whatIsDoing == VoiceUtil.IS_LISTEN_FINISH || VoiceUtil.whatIsDoing == VoiceUtil.IS_SPEAK_FINISH) {
                        threadCt = false;
                        Message message = new Message();
                        message.what = 0;
                        mHandler.sendMessage(message);
                    }
                }
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home :
                CallingActivity.this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
